﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using WebStudio.Business.Rule;
using WebStudio.Business.Entity;

public partial class Admin_CaseAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Admin_User"] == null || Session["Admin_User"].ToString() == "")
        {
            Response.Redirect("~/Admin/Login.aspx");
        }

        if (!Page.IsPostBack)
        {
            txtDate.Text = DateTime.Now.ToShortDateString();
            //ddlParentNavBind();
            GetArticleInfo();
        }
    }

    private ArticalEntity Info
    {
        get
        {
            if (string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                return null;
            }
            else
            {
                try
                {
                    return ArticalRule.GetArticalEntity(Convert.ToInt32(Request.QueryString["id"]));
                }
                catch
                {
                    return null;
                }
            }
        }
    }

    private void GetArticleInfo()
    {
        ArticalEntity articalEntity = Info;
        if (articalEntity != null)
        {
            txtArticleName.Text = articalEntity.ArticalTitle;
            txtArticleOriginal.Text = articalEntity.ArticalOriginal;
            chkIsShow.Checked = Convert.ToBoolean(articalEntity.Status);
            txtContent.Text = articalEntity.ArticalContent;
            //txtRank.Text = ArticleCategroyEntity.Rank.ToString();
            //chkIsBottom.Checked = Convert.ToBoolean(articalEntity.IsBottom);
            txtDate.Text = articalEntity.AddDate.ToString();
            txtTag.Text = articalEntity.ArticalTag.ToString();
            txtArticleOriginal.Text = articalEntity.ArticalOriginal.ToString();
            //if (articalEntity.PId == 0)
            //{
            //    ddlParentNav.SelectedValue = ArticleCategroyEntity.CategoryId.ToString();
            //}
            //else
            //{
            //    ddlSubNavBind(Convert.ToInt32(ArticleCategroyEntity.PId));
            //    ddlParentNav.SelectedValue = ArticleCategroyEntity.PId.ToString();
            //    ddlSubNav.SelectedValue = ArticleCategroyEntity.CategoryId.ToString();
            //}
        }
    }

    //private void ddlParentNavBind()
    //{
    //    ddlParentNav.DataSource = bCategory.GetParentList();
    //    ddlParentNav.DataTextField = "Name";
    //    ddlParentNav.DataValueField = "ID";
    //    ddlParentNav.DataBind();
    //    ddlParentNav.Items.Insert(0, new ListItem("请选择...", "0"));
    //    ddlSubNav.Items.Insert(0, new ListItem("请选择...", "0"));
    //}
    //private void ddlSubNavBind(int id)
    //{
    //    ddlSubNav.DataSource = bCategory.GetSubList(id);
    //    ddlSubNav.DataTextField = "Name";
    //    ddlSubNav.DataValueField = "ID";
    //    ddlSubNav.DataBind();
    //}

    //protected void ddlParentNav_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    int id = Convert.ToInt32(ddlParentNav.SelectedValue);
    //    if (id != 0)
    //    {
    //        ddlSubNavBind(id);
    //    }
    //    else
    //    {
    //        ddlSubNav.Items.Clear();
    //    }
    //    ddlSubNav.Items.Insert(0, new ListItem("请选择...", "0"));
    //}

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        bool fileCheck = false;
        string filename = GenerateFileName();
        if (fuFrdLinks.HasFile)
        {
            //判断上传文件的格式
            string fileExtension = System.IO.Path.GetExtension(fuFrdLinks.FileName).ToLower();
            string[] allowExtension = { ".gif", ".jpg", ".bmp", ".png" };
            for (int i = 0; i < allowExtension.Length; i++)
            {
                if (fileExtension == allowExtension[i])
                {
                    fileCheck = true;
                    break;
                }
            }
            if (fileCheck == false)
            {
                ApplicationHelper.ShowClientMessage(this, "上传图片的格式不对！");
                return;
            }
            //判断上传文件的大小
            if (fuFrdLinks.PostedFile.ContentLength > 2048000)
            {
                fileCheck = false;
                ApplicationHelper.ShowClientMessage(this, "您上传的文件超过2M！");
                return;
            }
            //上面所有的条件都符合，则执行上传操作
            if (fileCheck)
            {
                try
                {
                    string strFilePath = Server.MapPath("~/Images/case/");
                    if (!Directory.Exists(strFilePath))
                    {
                        Directory.CreateDirectory(strFilePath);
                    }
                    string fileSavePath = strFilePath + filename + fileExtension;
                    filename = filename + fileExtension;
                    fuFrdLinks.SaveAs(fileSavePath);
                }
                catch (Exception ex)
                {
                    ApplicationHelper.ShowClientMessage(this, ex.Message);
                }
            }
        }
        else
        {
            //ApplicationHelper.ShowClientMessage(this, "没有要上传的文件！");
            filename = "";
        }

        string title = txtArticleName.Text.Trim();
        string original = txtArticleOriginal.Text.Trim();
        string tag = txtTag.Text.Trim();
        string content = txtContent.Text.Trim();
        //string category = ddlParentNav.SelectedValue;
        if (title == "")
        {
            return;
        }
        if (original == "")
        {
            return;
        }
        if (content == "")
        {
            return;
        }
        ArticalEntity articleEntity = new ArticalEntity();
        articleEntity.ArticalTitle = title;
        articleEntity.ArticalOriginal = original;
        articleEntity.ArticalTag = tag;
        articleEntity.CategoryInfo.CategoryId = 3;
        articleEntity.InnerCode = "3";
        if (articleEntity != null && filename == "")
        {
            articleEntity.ArticalImage = Info.ArticalImage;
        }
        else
        {
            articleEntity.ArticalImage = "case/" + filename;
        }

        //articleEntity.ArticalImage = "case/" + filename;
        //if (ddlSubNav.Items.Count <= 1)
        //{
        //    articleEntity.CategoryId = Convert.ToInt32(category);
        //}
        //else
        //{
        //    articleEntity.CategoryId = Convert.ToInt32(ddlSubNav.SelectedValue);
        //}
        //if (string.IsNullOrEmpty(txtRank.Text.Trim()))
        //{
        //    articleEntity.Rank = 0;
        //}
        //else
        //{
        //    articleEntity.Rank = Convert.ToInt32(txtRank.Text.Trim());
        //}
        //articleEntity.IsBottom = chkIsBottom.Checked;
        articleEntity.UserInfo.UserId = 2;//暂时，记得修改
        if (chkIsShow.Checked)
        {
            articleEntity.Status = 1;
        }
        else
        {
            articleEntity.Status = 0;
        }
        articleEntity.AddDate = Convert.ToDateTime(txtDate.Text.Trim());
        articleEntity.ArticalContent = txtContent.Text.Trim();
        articleEntity.ArticalType = 0;
        articleEntity.UpdateDate = DateTime.Now;
        if (Info == null)
        {
            //bArticle.AddArticle(articleEntity);
            articleEntity.ArticalHits = 0;
            if (ArticalRule.SaveArticalInfor(articleEntity))
            {
                //ArticalRule.SaveArticalInfor(articleEntity);
                ApplicationHelper.AddStartupScript(this, "if(confirm('添加成功，是否继续添加？')){location.href='CaseAdd.aspx';}else{location.href='CaseMge.aspx';}");
            }
            else
            {
                ApplicationHelper.AddStartupScript(this, "alert('添加失败！')");
            }
        }
        else
        {
            articleEntity.ArticalId = Convert.ToInt32(Request.QueryString["id"]);
            ArticalRule.SaveArticalInfor(articleEntity);
            ApplicationHelper.AddStartupScript(this, "alert('修改成功');location.href='CaseMge.aspx';");
        }
    }

    //生成文件名
    private string GenerateFileName()
    {
        return string.Format("{0}{1}", Guid.NewGuid().ToString().Substring(0, 4), DateTime.Now.ToString("yyyyssMMhhmmdd"));
    }
}
